package com.ylc.jwtdemo.controller;

import com.ylc.jwtdemo.Utils.JwtUtils;
import com.ylc.jwtdemo.entity.User;
import com.ylc.jwtdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


//    登录
//    @GetMapping("/login")
    @PostMapping("/login")
    public Map<String,Object> login(User user) {
        log.info("用户名："+user.getUsername());
        log.info("密码："+user.getPassword());
        Map<String,Object> map=new HashMap<>();
        System.out.println(user);
        try {
            userService.login(user);
            map.put("msg","登录成功");
            map.put("code", "200");

            Map<String,String> payload=new HashMap<>();
            payload.put("name",user.getUsername());
            String token= JwtUtils.getToken(payload);
            map.put("token",token);
        }
        catch (Exception ex)
        {
            map.put("msg","登录失败");
        }

        return map;
    }

//    测试登录
    @PostMapping("/test")
    public  Map<String,String> verityToken() {
        Map<String, String> map=new HashMap<>();
        map.put("msg","验证成功");
        map.put("state","true");
        return map;
    }
//    退出登录
    @PostMapping("/logout")
    public  Map<String,String> logout() {
        Map<String, String> map=new HashMap<>();
        try {
            map.put("msg","退出登录成功");
            map.put("code", "200");
            map.put("token","");
        }
        catch (Exception ex)
        {
            map.put("msg","退出登录失败");
        }
        return map;
    }


}
